﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using KL.UFrame.Api.Extensions;
using KL.UFrame.Core.Result;
using KL.UFrame.Data.Business;
using KL.UFrame.Data.Business.Impl;
using KL.UFrame.Data.Models;

namespace KL.UFrame.Api.ApiControllers
{
    [RoutePrefix("Subscribe")]
    public class SubscribeController : K9ApiBaseController
    {
        private IPerformanceBusiness _business;

        public SubscribeController(IPerformanceBusiness business)
        {
            _business = business;
        }

        [Route("Subscribe")]
        public async Task<ResultMessage<bool>> SubscribeAsync(int performanceId, string openId)
        {
            return new ResultMessage<bool>(await _business.SubscribeAsync(performanceId, openId));
        }

        [Route("Unsubscribe")]
        public async Task<ResultMessage<bool>> UnsubscribeAsync(int performanceId, string openId)
        {
            return new ResultMessage<bool>(await _business.UnsubscribeAsync(performanceId, openId));
        }

        [HttpGet]
        [Route("Search")]
        public async Task<ResultMessage<List<SubscribingModel>>> SearchAsync(int performanceId, DateTime start,
            DateTime end, int pageIndex,
            int pageSize)
        {
            return
                await
                    _business.SearchSubscribingsAsync(performanceId, start.ToBeginning(), end.ToEnd(),
                        pageIndex < 1 ? 1 : pageIndex, pageSize < 1 ? 10 : pageSize);
        }
    }
}
